#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:zbsilent
__Author__: "zbsilent"

import os, sys

# data = open("yesterday", encoding="utf-8").read();
"""
f = open("yesterday", "a+", encoding="utf-8")  # 文件句柄
if os.path.isfile("yesterday"):

    f.write("\n"+"我爱北京天安门")  # 创建文件写法
    data = f.read()
    print(data)
f.close()
"""
# print(sys.path)  #打印环境变量
# print(sys.argv)  #打印绝对路径
"""
os modle is used create doc……

"""

# os.system(" 'hello'")  #执行命令行
# res = os.popen("say 'hello'").read()

#
# os.system()
# os.makedirs("20180104")
"""
This is demo


"""
# 数据列表
_list_comm = list()
# 行数据列表
_list_line = []
# 文件句柄
_file_list = open("yesterday", "r", encoding="utf-8")
# 所有商品信息列表行读取
_data = _file_list.readlines()


_num = 0
# 购物车信息
_gwc = []

# 是否可以参与购买
flag = True

for _each_row in _data:
    _row = _each_row.replace("\n", "")
    _str_list = _row.split(" ")
    _list_comm.append(_str_list)

_input_ = float(input("请输入你的工资......"))

count = 0

nexts = 0

while True:
    if nexts >= len(_list_comm):
        print("什么都买不起")
        flag = False
        break
    if count <= 1:
        for i in _list_comm:
            # print(i)
            # print(i[2])  # i[2:][0] 切片是get arrays
            if float(str(i[2]).replace("¥", "")) < _input_:
                print(
                    "商品编码：{_code}的{_name},价格是:{_numbers}".format(_code=str(i[0]), _name=str(i[1]), _numbers=str(i[2])))
            else:
                nexts = nexts + 1

            count = count + 1
    else:
        break

if flag:

    while True:

        _want = input("您要选择下单么？输入q退出，其他继续。。。。")

        if _want != "q":
            _inp_code = input("请您输入编号。。。")
            _inp_num = input("请您输入数量。。。")
            if _inp_code == "" or _inp_num == "":
                print("编号以及数量不能为空")
                break
            counts = 0
            while True:
                if counts > 3:
                    break;
                for d in _list_comm:
                    if d[0] == _inp_code and int(d[3]) > int(_inp_num) and (
                                float(str(d[2]).replace("¥", "")) * float(_inp_num)) < _input_:
                        _spz = list()
                        _spz.append(d)
                        _spz.append("购买数量{_sysl}".format(_sysl=int(_inp_num)))
                        _gwc.append(_spz)
                        _input_ = _input_ - (float(str(d[2]).replace("¥", "")) * float(_inp_num))
                        print("剩余工资={_ppt}".format(_ppt=_input_))
                        if _input_ < 0:
                            print("请支付足够的钱,负责请修改数量")
                    elif d[0] == _inp_code and (float(str(d[2]).replace("¥", "")) * float(_inp_num)) < _input_:
                        print("数量超过存货数量")
                    elif d[0] == _inp_code:
                        print("钱不够")
                    counts = counts + 1
        else:

            break
    if len(_gwc) == 0:
        print("什么都不选买毛")
    else:
        strs = "剩余金额:{_syje}".format(_syje=_input_)
        _gwc.append(strs)
        print(_gwc)
